package com.loan.market.ui

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.loan.market.R
import com.loan.market.base.BaseBindingActivity
import com.loan.market.databinding.ActivityMainBinding
import com.loan.market.util.transparentStatusBar

class MainActivity : BaseBindingActivity<ActivityMainBinding>() {

    override fun getViewBinding(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    private val homeFragment by lazy { supportFragmentManager.findFragmentByTag("home") ?: HomeFragment() }
    private val meFragment by lazy { supportFragmentManager.findFragmentByTag("me") ?: MeFragment() }

    override fun initUi(savedInstanceState: Bundle?) {
        transparentStatusBar()
        switchPage(homeFragment, meFragment, "home")
    }

    override fun initEvent() {
        binding.rbTab.setOnCheckedChangeListener { group, checkedId ->
            if (checkedId == R.id.rb_home) {
                switchPage(homeFragment, meFragment, "home")
            } else {
                switchPage(meFragment, homeFragment, "me")
            }
        }
    }

    private fun switchPage(show: Fragment, hide: Fragment, tag: String) {
        supportFragmentManager.beginTransaction().apply {
            if (!show.isAdded) add(R.id.fl_content, show, tag)
            if (hide.isAdded) hide(hide)
            show(show).commit()
        }
    }


    override fun initData() {

    }
}
